﻿//-----------------------------------------------------------------------
// <copyright file="SaveSettingsCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 
namespace ChatBox.UI.Windows.Settings
{
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;

    using Themes;

    /// <summary>
    /// Command to save settings
    /// </summary>
    public class SaveSettingsCommand : GenericCommandBase<SettingsWindowViewModel>
    {
        /// <summary>
        /// The source of server settings
        /// </summary>
        private readonly IServerSettingsRepository serverSettingsRepository;

        /// <summary>
        /// The source of user settings
        /// </summary>
        private readonly IUISettingsRepository uiSettingsRepository;

        /// <summary>
        /// The theme manager
        /// </summary>
        private readonly ThemeManager themeManager;

        /// <summary>
        /// Initializes a new instance of the SaveSettingsCommand class
        /// </summary>
        /// <param name="serverSettingsRepository">The source of server settings</param>
        /// <param name="uiSettingsRepository">The source of ui settings</param>
        /// <param name="themeManager">The theme manager</param>
        public SaveSettingsCommand(IServerSettingsRepository serverSettingsRepository, IUISettingsRepository uiSettingsRepository, ThemeManager themeManager)
        {
            this.serverSettingsRepository = serverSettingsRepository;
            this.uiSettingsRepository = uiSettingsRepository;
            this.themeManager = themeManager;
        }

        /// <summary>
        /// Execute the "save settings" command
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(SettingsWindowViewModel value)
        {
            ServerSettingsData serverSettingsData = this.serverSettingsRepository.ReadSettings();
            UISettingsData uiSettingsData = this.uiSettingsRepository.ReadSettings();

            // save the new settings
            bool serverSettingsChanged = !ServerSettingsDataHelper.SettingsSame(serverSettingsData, value.ServerSettings);
            bool uiSettingsChanged = !UISettingsDataHelper.SettingsSame(uiSettingsData, value.UISettings);

            if (serverSettingsChanged)
            {
                this.serverSettingsRepository.SaveSettings(value.ServerSettings);
            }

            if (uiSettingsChanged)
            {
                this.uiSettingsRepository.SaveSettings(value.UISettings);

                this.themeManager.CurrentTheme = value.UISettings.Theme;
            }

            if (serverSettingsChanged || uiSettingsChanged)
            {
                if (value.NotifyApplicationOfSettingsChange != null)
                {
                    value.NotifyApplicationOfSettingsChange.Execute(value);
                }
            }

            value.Window.Close();
        }

        /// <summary>
        /// Hack to stop it being falsely disabled
        /// </summary>
        /// <param name="parameter">the object to test</param>
        /// <returns>always true</returns>
        public override bool CanExecute(object parameter)
        {
            return true;
        }
    }
}
